package com.game_ltdd;

import java.util.LinkedList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.game.framwork.itf.Audio;
import com.game.framwork.itf.FileIO;
import com.game.framwork.itf.Game;
import com.game.framwork.itf.Graphics;
import com.game.framwork.itf.Input;
import com.game.framwork.itf.Screen;
import com.game.framwork.mng.GameAudio;
import com.game.framwork.mng.GameFastRenderView;
import com.game.framwork.mng.GameFileIO;
import com.game.framwork.mng.GameGraphics;
import com.game.framwork.mng.GameInput;
import com.game.screen.BattleScreen;
import com.game.screen.MenuScreen;

public class GameGame extends Activity implements Game {
	private static GameGame instance;
	
	public GameGame() {
		super();
		instance = this;
	}
	
	public static GameGame getGame() {
		return instance;
	}
	GameFastRenderView renderView;
	Graphics graphics;
	Audio audio;
	Input input;
	FileIO fileIO;
	Screen screen;
	WakeLock wakeLock;
	AlertDialog.Builder alert;
	LinkedList<Screen> listScreens;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		boolean isLandscape = getResources().getConfiguration().orientation ==
				Configuration.ORIENTATION_LANDSCAPE;
		int frameBufferWidth = isLandscape ? 800 : 480; 
		int frameBufferHeight = isLandscape ? 480 : 800;
		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
				frameBufferHeight, Config.RGB_565);
		
		float scaleX = (float) frameBufferWidth
				/ getWindowManager().getDefaultDisplay().getWidth();
		float scaleY = (float) frameBufferHeight
				/ getWindowManager().getDefaultDisplay().getHeight();
		
		renderView = new GameFastRenderView(this, frameBuffer);
		graphics = new GameGraphics(getAssets(), frameBuffer);
		fileIO = new GameFileIO(getAssets());
		audio = new GameAudio(this);
		input = new GameInput(this, renderView, scaleX, scaleY);
		listScreens = new LinkedList<Screen>();
		initScreen();
		screen = getStartScreen();
		setContentView(renderView);
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GLGame");
		wakeLock.acquire();
	}
	 
	@Override
	public void onResume() {
		super.onResume();
		wakeLock.acquire();
		screen.resume();
		renderView.resume();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		renderView.pause();
		screen.pause();
		listScreens.clear();
		if(this.wakeLock != null && this.wakeLock.isHeld()) {
			this.wakeLock.release();	
		}
		if (isFinishing()) {
			screen.dispose();
		}
			
	}
	
	public Input getInput() {
		return input;
	}
	
	public FileIO getFileIO() {
		return fileIO;
	}
	
	public Graphics getGraphics() {
		return graphics;
	}
	
	public Audio getAudio() {
		return audio;
	}
	
	public void initScreen() {
		MenuScreen menuScreen = new MenuScreen(this);
		listScreens.add(Screen.SCREEN_MENU, menuScreen);
		BattleScreen battleScreen = new BattleScreen(this);
		listScreens.add(Screen.SCREEN_BATTLE, battleScreen);
	}
	
	@Override
	public void jumScreen(int id) {
		this.screen.pause();
		this.screen = listScreens.get(id);
	}
	
	public void setScreen(Screen screen) {
		if (screen == null)
			throw new IllegalArgumentException("Screen must not be null");
		
		this.screen.pause();
		this.screen.dispose();
		screen.resume();
		screen.update(0);
		this.screen = screen;
	}
	
	public Screen getCurrentScreen() {
		return screen;
	}
	
	public Screen getStartScreen() {
		return listScreens.get(Screen.SCREEN_MENU);
	}

	@Override
	public int getDeviceWidth() {
		return getWindowManager().getDefaultDisplay().getWidth();
	}

	@Override
	public int getDeviceHeight() {
		return getWindowManager().getDefaultDisplay().getHeight();
	}
}
